package net.jwtools.tmt.service;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import net.jwtools.tmt.dao.hibernate.VisitHibernateDAO;
import net.jwtools.tmt.domain.IVisit;
import net.jwtools.tmt.domain.MainVisit;

import org.jfree.data.time.Week;

/**
 * @author Naftali van der Loon
 * 
 * This service to Persons
 */

public class VisitService implements TMTService, IVisitService {
	private net.jwtools.tmt.dao.hibernate.VisitHibernateDAO dao;

	public void setDao(VisitHibernateDAO visitDAO) {
		this.dao = visitDAO;
	}

	public VisitHibernateDAO getDao() {
		return dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jwtools.tmt.service.IVisitService#countAllCompletedMainVisits()
	 */
	public Integer countAllCompletedMainVisits() {
		return dao.countAllCompletedMainVisits();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jwtools.tmt.service.IVisitService#countAllCompletedReVisits()
	 */
	public Integer countAllCompletedReVisits() {
		return dao.countAllCompletedReVisits();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jwtools.tmt.service.IVisitService#countAllOutstandingReVisits()
	 */
	public Integer countAllOutstandingReVisits() {
		return dao.countAllOutstandingReVisits();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jwtools.tmt.service.IVisitService#countAllOutstandingMainVisits()
	 */
	public Integer countAllOutstandingMainVisits() {
		return dao.countAllOutstandingMainVisits();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jwtools.tmt.service.IVisitService#getAllCompleted()
	 */
	public List<IVisit> getAllCompleted() {
		List<IVisit> visits = null;
		visits = dao.findAllCompleted();
		return visits;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jwtools.tmt.service.IVisitService#save(net.jwtools.tmt.domain.Visit)
	 */
	public void save(IVisit visit) {
			dao.saveOrUpdate(visit);
	}

	@Override
	public Map<Week, Integer> getRegularVisitsHistoryByWeek() {
		GregorianCalendar now = new GregorianCalendar();
		GregorianCalendar sixMonthsAgo = new GregorianCalendar();
		sixMonthsAgo.add(Calendar.MONTH, -6);

		return dao.findCheckedOutMainVisitsBetween(sixMonthsAgo.getTime(), now
				.getTime());
	}

	@Override
	public Map<Week, Integer> getReVisitsHistoryByWeek() {
		GregorianCalendar now = new GregorianCalendar();
		GregorianCalendar sixMonthsAgo = new GregorianCalendar();
		sixMonthsAgo.add(Calendar.MONTH, -6);

		return dao.findCheckedOutReVisitsBetween(sixMonthsAgo.getTime(), now
				.getTime());

	}

	@Override
	public List<IVisit> getAllOutstanding() {
		return dao.findAllOutstanding();
	}

	public List<IVisit> getVisitsBelongingTo(IVisit visit) {
		IVisit a = dao.find(visit.getId());
		List<IVisit> visits = dao.findAllBelongingTo(visit);
		visits.add(0,visit);
		return visits;
	}

}
